package realtime.util;/*
 *@ClassName : DimUtil
 *@Description : $ {description}
 *@Author : NingYu
 *@Date : 2023-03-24 11:{MINUTE}
 */

import com.alibaba.fastjson.JSONObject;
import realtime.common.GmallConfig;
import redis.clients.jedis.Jedis;


import java.sql.Connection;
import java.util.List;


public class DimUtil {

    public static JSONObject getDimInfo(Connection connection,String tableName,String id) throws Exception {
        //查询redis
        Jedis jedis = JedisUtil.getJedis();
        String redisKey = "DIM:"+tableName+":"+id;
        String dimInfoStr = jedis.get(redisKey);

        if (dimInfoStr != null){
            jedis.expire(redisKey,24*3600);
            jedis.close();
            return JSONObject.parseObject(dimInfoStr);
        }
        String sql = "select * from "+ GmallConfig.HBASE_SCHEMA+"."+tableName+" where id = '"+id+"'";

        List<JSONObject> queryList = JdbcUtil.queryList(connection, sql, JSONObject.class, false);

        JSONObject dimInfo = queryList.get(0);
        jedis.setex(redisKey,24*3600,dimInfo.toJSONString());
        jedis.close();
        return dimInfo;
    }
}